package com.imooc.miaosha.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;

import java.util.Map;

public class JsonUtil<T> {
    public static <T> String Obj2String(T t) {

        return JSON.toJSONString(t);
    }

    public static <T> T String2Obj(String str, TypeReference<T> typeReference) {
        return typeReference.getType().equals(String.class) ? (T) str : JSON.parseObject(str, typeReference);
    }

    public static <T> T map2Obj(Map<String, Object> map, String key, Class<T> clazz) {
        Object o = map.get(key);
        String oJson = JSON.toJSONString(o);
        return JSON.parseObject(oJson, clazz);
    }
}
